package com.kit.data.kitopeso.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class KitoSpaceItemDecoration1 extends RecyclerView.ItemDecoration {

    private final int horizontalSpacing;
    private final int verticalSpacing;

    public KitoSpaceItemDecoration1(int horizontalSpacingDp, int verticalSpacingDp, Context context) {
        this.horizontalSpacing = dpToPx(horizontalSpacingDp, context);
        this.verticalSpacing = dpToPx(verticalSpacingDp, context);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);


        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager || layoutManager instanceof StaggeredGridLayoutManager) {

            handleGridSpacing(outRect, parent.getChildAdapterPosition(view), layoutManager);
        } else if (layoutManager instanceof LinearLayoutManager) {

            handleLinearSpacing(outRect, parent.getChildAdapterPosition(view), layoutManager);
        }
    }

    private void handleLinearSpacing(Rect outRect, int position, RecyclerView.LayoutManager layoutManager) {
        int orientation = ((LinearLayoutManager) layoutManager).getOrientation();
        if (orientation == LinearLayoutManager.VERTICAL) {
            outRect.left = horizontalSpacing;
            outRect.right = horizontalSpacing;
            outRect.bottom = verticalSpacing;
            if (position == 0) outRect.top = verticalSpacing;
        } else {
            outRect.top = verticalSpacing;
            outRect.bottom = verticalSpacing;
            outRect.right = horizontalSpacing;
            if (position == 0) outRect.left = horizontalSpacing;
        }
    }

    private void handleGridSpacing(Rect outRect, int position, RecyclerView.LayoutManager layoutManager) {
        int spanCount = getSpanCount(layoutManager);
        int column = position % spanCount;


        outRect.bottom = verticalSpacing / 2;

        outRect.left = horizontalSpacing / 2;
        outRect.right = horizontalSpacing / 2;


        if (position == 0) {
            outRect.top = verticalSpacing;
        } else {
            outRect.top = verticalSpacing / 2;
        }


    }


    private int dpToPx(int dp, Context context) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()
        );
    }


    private int getSpanCount(RecyclerView.LayoutManager layoutManager) {
        if (layoutManager instanceof GridLayoutManager) {
            return ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return 1;
    }

}
